/***********************************************************************************
 * 文件名： mcu_adv_timer.c
 * 作者： 刘言
 * 版本： 1
 * 说明：
 * 		高级定时器驱动。
 * 修改记录：
 * 	2024/9/11: 初版。 刘言。
***********************************************************************************/
#include "mcu_adv_timer.h"

#define _TIM1_DIR       0   // 0: 向上计数
#define _TIM1_CMS       0   // 0:边沿对齐模式。计数器依据方向位(DIR)向上或向下计数。
#define _TIM1_ARPE      0   // 0： TIM1_ARR 寄存器没有缓冲。1： TIM1_ARR 寄存器被装入缓冲器
#define _TIM1_CCPC      0   // 0: CcxE， CcxNE 和 OCxM 位不是预装载的。
#define _TIM1_CCUS      0   // 0: 如果捕获/比较控制位是预装载的(CCPC=1)，只能通过设置 COM 位更新它们。
#define _TIM1_MMS       0   // 主模式选择
#define _TIM1_TI1S      0   // 异或输出功能使能




void AdvTim_Init()
{
    TIM1->CR1 = (_TIM1_DIR << 4) |
                (_TIM1_CMS << 5) |
                (_TIM1_ARPE << 7);
    TIM1->CR2 = (_TIM1_CCPC << 0) |
                (_TIM1_CCUS << 2) |
                (_TIM1_MMS << 4) |
                (_TIM1_TI1S << 7) |
                (_TIM1_OIS1 << 8) |
                (_TIM1_OIS1N << 9) |
                (_TIM1_OIS2 << 10) |
                (_TIM1_OIS2N << 11) |
                (_TIM1_OIS3 << 12) |
                (_TIM1_OIS3N << 13) |
                (_TIM1_OIS4 << 14);
    TIM1->CCMR1 = (_TIM1_CC1S << 0) |
#if (_TIM1_CC1S == 0)
                (_TIM1_OC1FE << 2) |
                (_TIM1_OC1PE << 3) |
                (_TIM1_OC1M << 4) |
                (_TIM1_OC1CE << 7) |
#else
                (_TIM1_IC1PSC << 2) |
                (_TIM1_IC1F << 4) |
#endif
                (_TIM1_CC2S << 8) |
#if (_TIM1_CC2S == 0)
                (_TIM1_OC2FE << 10) |
                (_TIM1_OC2PE << 11) |
                (_TIM1_OC2M << 12) |
                (_TIM1_OC2CE << 15);
#else
                (_TIM1_IC2PSC << 2) |
                (_TIM1_IC2F << 4);
#endif
    TIM1->CCMR2 = (_TIM1_CC3S << 0) |
#if (_TIM1_CC3S == 0)
                (_TIM1_OC3FE << 2) |
                (_TIM1_OC3PE << 3) |
                (_TIM1_OC3M << 4) |
                (_TIM1_OC3CE << 7) |
#else
                (_TIM1_IC3PSC << 2) |
                (_TIM1_IC3F << 4) |
#endif
                (_TIM1_CC4S << 8) |
#if (_TIM1_CC4S == 0)
                (_TIM1_OC4FE << 10) |
                (_TIM1_OC4PE << 11) |
                (_TIM1_OC4M << 12) |
                (_TIM1_OC4CE << 15);
#else
                (_TIM1_IC4PSC << 2) |
                (_TIM1_IC4F << 4);
#endif
    TIM1->CCER = (_TIM1_CC1E << 0) |
                (_TIM1_CC1P << 1) |
                (_TIM1_CC1NE << 2) |
                (_TIM1_CC1NP << 3) |
                (_TIM1_CC2E << 4) |
                (_TIM1_CC2P << 5) |
                (_TIM1_CC2NE << 6) |
                (_TIM1_CC2NP << 7) |
                (_TIM1_CC3E << 8) |
                (_TIM1_CC3P << 9) |
                (_TIM1_CC3NE << 10) |
                (_TIM1_CC3NP << 11) |
                (_TIM1_CC4E << 12) |
                (_TIM1_CC4P << 13);
    TIM1->PSC = _TIM1_PSC;
    TIM1->ARR = _TIM1_ARR;
    // TIM1->RCR?
    /* Generate an update event to reload the Prescaler
     and the repetition counter value (if applicable) immediately */
    SET_BIT(TIM1->EGR, TIM_EGR_UG);
    TIM1->SR = 0;
    TIM1->DIER = (_TIM1_UIE << 0) |
                (_TIM1_CC1IE << 1) |
                (_TIM1_CC2IE << 2) |
                (_TIM1_CC3IE << 3) |
                (_TIM1_CC4IE << 4) |
                (_TIM1_COMIE << 5) |
                (_TIM1_TIE << 6) |
                (_TIM1_BIE << 7);
    TIM1->CCR1 = _TIM1_CCR1;
    TIM1->CCR2 = _TIM1_CCR2;
    TIM1->CCR3 = _TIM1_CCR3;
    TIM1->CCR4 = _TIM1_CCR4;
    TIM1->BDTR = (_TIM1_DTG << 0) |
                (_TIM1_OSSI << 10) |
                (_TIM1_OSSR << 11) |
                (_TIM1_MOE << 15);
#if _TIM1_RUN == 1
    SET_BIT(TIM1->CR1, TIM_CR1_CEN);
#endif
}



